$(function () {
    //解除绑定
    container.off("click");
    function getLabRegistrations(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "teacherName":$("#teacherName").val(),
                "className":$("#className").val(),
                "classroomName":$("#classroomName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getLabRegistrations(page);
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getLabRegistrations(0);
    });

    //获取编辑实验室登记界面
    container.on("click","#labRegistration-edit",function () {
        $("#labRegistrationTitle").html("编辑实验室登记");
        axios({
            method:'get',
            url:"/labRegistration/edit/"+$(this).attr('labRegistrationId')
        }).then(function (response) {
            $("#labRegistrationForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });


    //提交操作，清空表单
    $("#labRegistrationForm").submit(function() {
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:"POST",
            url:"/labRegistration",
            data:$('#labRegistrationForm').serialize(),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#labRegistrationForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getLabRegistrations(0);
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}
        }).catch(function (error) {
            toastr.error("error!");
        });

        $("#labRegistrationDialogClose").click();
        return false;
    });


    //关闭Dialog界面
    $("#labRegistrationDialogClose").click(function () {
          $("#labRegistrationForm").html("");
    });

});